from django import forms
from .bootstrap import BootStrapFrom
from web import models


class IssuesModelForm(BootStrapFrom, forms.ModelForm):
    class Meta:
        model = models.Issues
        exclude = ['project', 'creator', 'create_datetime', 'latest_update_datetime']
        # 给字段添加属性
        widgets = {
            # 选择器，搜索功能
            'assign': forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
            'attention': forms.SelectMultiple(
                attrs={'class': "selectpicker", "data-live-search": "true", "data-actions-box": "true"}),
            'parent': forms.Select(attrs={'class': "selectpicker", "data-live-search": "true"}),
        }

        # 表单 带选项的数据初始化

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 1.获取该项目所有拥有的问题类型 （不能为空）
        issues_type_obj_list = models.IssuesType.objects.filter(project=request.tracer.project).values_list('id',
                                                                                                            'title')
        self.fields['issues_type'].choices = issues_type_obj_list

        # 2.获取该项目所有的模块 可以为空
        module_list = [('', '没有选中任何项')]
        module_obj_list = models.Module.objects.filter(project=request.tracer.project).values_list('id', 'title')
        module_list.extend(module_obj_list)
        self.fields['module'].choices = module_list

        # 3. 指派者和关注者 （当前项目的参与者和创建者）
        total_list = []
        # 当前项目创建者
        creator_list = [(request.tracer.project.creator.id, request.tracer.project.creator.username)]
        total_list.extend(creator_list)
        # 当前项目的参与者
        project_user_list = models.ProjectUser.objects.filter(project=request.tracer.project).values_list('user__id',
                                                                                                          'user__username')
        total_list.extend(project_user_list)

        self.fields['assign'].choices = [('', '没有选中任何项')] + total_list
        self.fields['attention'].choices = total_list

        # 获取当前项目所有的父问题
        parent_list = [('', '没有选中任何项')]
        parent_obj_list = models.Issues.objects.filter(project=request.tracer.project).values_list('id',
                                                                                                   'subject')
        parent_list.extend(parent_obj_list)
        self.fields['parent'].choices = parent_list


class IssuesReplayModelForm(forms.ModelForm):
    class Meta:
        model = models.IssueReplay
        fields = ['content','reply']


# 项目成员邀请码
class ProjectInviteModelForm(BootStrapFrom,forms.ModelForm):
    class Meta:
        model = models.ProjectInvite
        fields = ['period','count']